package demo;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-07-15
 * Time: 21:30
 */
public class TestDemo {

    public static void heapSort(int[] arr){
        if(arr==null||arr.length<2){
            return;
        }

        for (int i = arr.length - 1; i >= 0; i--) {
            heapIfy(arr, i, arr.length);
        }

        int heapSize = arr.length;
        while(heapSize>0){
            swap(arr,0,--heapSize);//堆顶元素与最后一个交换
            heapIfy(arr,0,heapSize);//不断调整0下标
        }
    }

    private static void heapIfy(int[] arr,int parent,int len){
        int child = 2*parent + 1;//左孩子
        while(child<len){
            //选择孩子中较大的节点
            if(child+1<len&&arr[child]<arr[child+1]){
                 child++;
            }
            //孩子节点与父亲节点比较
            if(arr[child]<=arr[parent]){
                break;
            }
            swap(arr,child,parent);
            //更新父亲节点和孩子节点
            parent = child;
            child = 2*parent+1;
        }
    }

    public static void swap(int[] arr,int i,int j){
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    public static void comparator(int[] arr){
        Arrays.sort(arr);
    }


    public static int[] generateRandomArray(int maxSize,int maxValue){
        int[] arr = new int[(int)((maxSize+1)*Math.random())];
        for(int i =0;i<arr.length;++i){
            arr[i] = (int)((maxValue+1)*Math.random()) - (int)((maxValue)*Math.random());
        }
        return arr;
    }

    public static int[] copyArray(int[] arr){
        if(arr==null) return null;
        int[] tmp = new int[arr.length];
        for(int i =0;i<arr.length;++i){
            tmp[i] = arr[i];
        }
        return tmp;
    }

    public static boolean isEquals(int[] arr1,int[] arr2){
         if((arr1==null&&arr2!=null)||(arr1!=null&&arr2==null)){
              return false;
         }
         if(arr1==null&&arr2==null) {
             return true;
         }
         if(arr1.length!=arr2.length){
             return false;
         }
         for(int i=0;i<arr1.length;++i){
             if(arr1[i]!=arr2[i]){
                 return false;
             }
         }
         return true;
    }

    public static void main(String[] args) {
        int testTime = 5000;
        int maxSize = 100;
        int maxValue = 100;
        boolean succeed = true;
        for(int i =0;i<testTime;++i){
            int[] arr1 = generateRandomArray(maxSize,maxValue);
            int[] arr2 = copyArray(arr1);
            heapSort(arr1);
            comparator(arr2);
            if(!isEquals(arr1,arr2)){
                succeed = false;
                System.out.println(Arrays.toString(arr1));
                System.out.println(Arrays.toString(arr2));
                break;
            }
        }
        System.out.println(succeed==true?"nice":"fuck");
    }


}
